package com.raysdata.smartcity.dao.sys;

import com.raysdata.smartcity.dao.RzBaseDao;
import com.raysdata.smartcity.dto.Organization;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by bh on 2016/11/21.
 */
@Component
public class OrgDao {
    @Autowired
    private RzBaseDao rzBaseDao;

    public List<Organization> getOrgListByParentId(String parentId){
        String sql="select * from ss_org where status='0' and parent_id=? order by sort_no asc ";
        return rzBaseDao.query(sql,Organization.class,new Object[]{parentId});
    }

    public Organization getOrgListByParentNull(){
        String sql="select * from ss_org where parent_id is null order by sort_no asc ";
        return rzBaseDao.queryById(sql,Organization.class);
    }
    public Organization getOrgById(String org_id){
        String sql="select * from ss_org where org_id=? ";
        return rzBaseDao.queryById(sql,Organization.class,org_id);
    }
    public int insert(Organization org){
        StringBuilder sql = new StringBuilder();
        sql.append("insert into ss_org  ( org_id,  ");
        sql.append("parent_id , ");
        sql.append("org_code, ");
        sql.append("name,");
        sql.append("cperson,");
        sql.append("cdate,");
        sql.append("sort_no,");
        sql.append("is_dept,");
        sql.append("full_name,");
        sql.append("status");

        sql.append(") values( ");
        sql.append(":org_id , ");
        sql.append(":parent_id,");
        sql.append(":org_code,");
        sql.append(":name,");
        sql.append(":cperson,");
        sql.append(":cdate,");
        sql.append(":sort_no,");
        sql.append(":is_dept,");
        sql.append(":full_name,");
        sql.append(":status ");
        sql.append(")");
        rzBaseDao.insert(sql.toString(), org);
        return 0;
    }
    public int update(Organization org){
        StringBuilder sql = new StringBuilder();
        sql.append("update ss_org set name=:name,full_name=:full_name,sort_no=:sort_no,is_dept=:is_dept where org_id=:org_id ");
        return rzBaseDao.update(sql.toString(),org);
    }
    public int delete(Organization org){
        String sql="update ss_org set status='1' where org_code like '"+org.getOrg_code()+"%'";
        return rzBaseDao.update(sql,org);
    }

    /**
     * 根据org_id查找parent不是部门的
     * @param org_id
     * @return
     */
    public Organization getParentOrg(String org_id){
        Organization org = getOrgById(org_id);
        return isDept(org);
    }

    public Organization isDept(Organization org){

        if(org.getParent_id()==null || org.getParent_id()==""){
            return org;
        }else{

            Organization org1 = getOrgById(org.getParent_id());

            if(org1.getIs_dept().equals("1")){
                return isDept(org1);
            }else{
                return org1;
            }
        }
    }


}
